package logic;

import java.text.DecimalFormat;
import java.util.Calendar;

import javax.swing.JOptionPane;

import ui.Screen;

/** Determines the future interest rate for the chosen account
 * @author Matthias
 *
 */
public class InterestRate extends Transaction {

	/** variable interestRate **/
	private double interestRate;
 
	/** Invokes the overridden superclass's constructor perform
	 * @param perform
	 */
	public InterestRate(final PerformTransactions perform) {
		super(perform);	
	}
	
	/** gives values to the interest rate for the unique account types
	 * @param accountType
	 * @return returns the matching interest rate to the chosen account type
	 */
	public double getInterestRate(final String accountType){ 
		
		if (accountType.equals("Credit")) {
			this.interestRate = 0.0125;
		}
		else if (accountType.equals("Savings")) {
			this.interestRate = 0.0250;
		}
		else if (accountType.equals("Depot")) {
			this.interestRate = 0.0450;
			
		}
		return interestRate;
	}
	
	/** takes action from Transaction and corresponding from PerformTransactions
	 * @see logic.Transaction#execute()
	 * getting the method executeIR() and inserting it in the ui class AccInterestRate method showTextArea
	 */
	@Override 
	public void execute(){ 
		getClassPerform().getClassATM().getClassScreen().getAccInterestRate().showTextArea(executeIR());
	}
	
	/** 
	 * @return the future amount for the next 10 years of the chosen account
	 */ 
	public String executeIR(){

		// instantiating a StringBuilder in order to append the results to the JTextArea
		StringBuilder sb = new StringBuilder();
		getClassPerform().getClassATM().getClassScreen().show(Screen.ACCINTERESTRATE);
		int endyear = 
				getClassPerform().getClassATM().getClassScreen().getAccInterestRate().getEndYear(); 
		// retrieves the current year of the computer calendar
		int currentYear = 
				Calendar.getInstance().get(Calendar.YEAR); // Gets the current year from the computer
		// gets the current balance of the chosen account
		double currentBalance = 
				getClassPerform().getClassATM().getClassBankDatabase().getTotalBalance(getUserID());

		DecimalFormat dec = new DecimalFormat("#.00");
		// calculates the balance each year up until the year chosen depending on the interest rate
		if (endyear <= (currentYear+10) && endyear >= currentYear) {
			for (int year = currentYear; year<=endyear; year++) {
				double balance = currentBalance * Math.pow(1 + getInterestRate(getAccType()), year-currentYear);
				sb.append(dec.format(balance) + " - " + year + "\n");
			} // end for
		} // end if
		// if the end year is before the current year or,
		// the end year is more than 10 years from the current year 
		// it is determined invalid
		else if (endyear < currentYear || endyear > (currentYear+10)) {
			JOptionPane.showMessageDialog(null, "Invalid input, You must enter an integer from "
					+ currentYear + " until " + (currentYear+10), "Error!", JOptionPane.ERROR_MESSAGE);
		}
		return sb.toString();
	} // end method

} // end class InterestRate